package com.starsoft.smdc.crawler.sina;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starsoft.frame.fileimport.CsvReader;
import com.starsoft.frame.fileimport.pojo.Column;
import com.starsoft.frame.fileimport.pojo.DataTable;
import com.starsoft.frame.fileimport.pojo.NullType;
import com.starsoft.frame.fileimport.pojo.Table;
import com.starsoft.frame.fileimport.pojo.Table.TitleType;
import com.starsoft.frame.fileimport.pojo.ValueType;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcHandicap;
import com.starsoft.smdc.bean.SmdcMarketDaily;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.common.HandicapCrawler;


public class HandicapSina extends SinaService implements HandicapCrawler{

	private static Logger logger = LoggerFactory.getLogger(HandicapSina.class);

	@Override
	public SmdcHandicap getHandicap(SmdcSecurity security) {
		List<SmdcHandicap> handicapList=  this.getHandicap(Arrays.asList(security));
		if(handicapList.size()>0) return handicapList.get(0);
		return null;
	}
	
	@Override
	public List<SmdcHandicap> getHandicap(List<SmdcSecurity> securityList){
		if(securityList==null || securityList.isEmpty()){
			return new ArrayList<>();
		}
		String secIds = "";
		for(SmdcSecurity security:securityList){
			secIds += convertSecId(security.getSecId(), false)+",";
		}
		return getHandicap(secIds);
	}
	
	private List<SmdcHandicap> getHandicap(String secIds) {
		List<SmdcHandicap> handicapList = new ArrayList<SmdcHandicap>();
		String apiName = "handicap";
		String response = this.request(apiName, secIds);
		if(StringUtil.isEmpty(response)){
			throw new RuntimeException("netease handicap no response");
		}
		String listValue = parseToList(response);

		List<Column> columnList = new ArrayList<Column>();
		columnList.add(new Column(0, "secId", ValueType.String, NullType.notnull));
		columnList.add(new Column(4, "price", ValueType.Double, NullType.nullable));
		columnList.add(new Column(2, "openPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column(3, "preClosePrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column(5, "highPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column(6, "lowPrice", ValueType.Double, NullType.nullable));
//		columnList.add(new Column("percent", "percent", ValueType.Double, NullType.nullable));
//		columnList.add(new Column("updown", "priceChange", ValueType.Double, NullType.nullable));
		columnList.add(new Column(9, "turnoverVol", ValueType.Double, NullType.nullable));
		columnList.add(new Column(10, "turnoverValue", ValueType.Double, NullType.nullable));
//		columnList.add(new Column("turnoverRate", "turnoverRate", ValueType.Double, NullType.nullable));

		columnList.add(new Column(12, "buy1", ValueType.Double, NullType.nullable));
		columnList.add(new Column(14, "buy2", ValueType.Double, NullType.nullable));
		columnList.add(new Column(16, "buy3", ValueType.Double, NullType.nullable));
		columnList.add(new Column(18, "buy4", ValueType.Double, NullType.nullable));
		columnList.add(new Column(20, "buy5", ValueType.Double, NullType.nullable));
		columnList.add(new Column(11, "buyVol1", ValueType.Double, NullType.nullable));
		columnList.add(new Column(13, "buyVol2", ValueType.Double, NullType.nullable));
		columnList.add(new Column(15, "buyVol3", ValueType.Double, NullType.nullable));
		columnList.add(new Column(17, "buyVol4", ValueType.Double, NullType.nullable));
		columnList.add(new Column(19, "buyVol5", ValueType.Double, NullType.nullable));

		columnList.add(new Column(22, "sell1", ValueType.Double, NullType.nullable));
		columnList.add(new Column(24, "sell2", ValueType.Double, NullType.nullable));
		columnList.add(new Column(26, "sell3", ValueType.Double, NullType.nullable));
		columnList.add(new Column(28, "sell4", ValueType.Double, NullType.nullable));
		columnList.add(new Column(30, "sell5", ValueType.Double, NullType.nullable));
		columnList.add(new Column(21, "sellVol1", ValueType.Double, NullType.nullable));
		columnList.add(new Column(23, "sellVol2", ValueType.Double, NullType.nullable));
		columnList.add(new Column(25, "sellVol3", ValueType.Double, NullType.nullable));
		columnList.add(new Column(27, "sellVol4", ValueType.Double, NullType.nullable));
		columnList.add(new Column(29, "sellVol5", ValueType.Double, NullType.nullable));

		columnList.add(new Column(31, "date", ValueType.String, NullType.nullable));
		columnList.add(new Column(32, "time", ValueType.String, NullType.nullable));
		
		DataTable table = new CsvReader().read(listValue, new Table(TitleType.Null, columnList));
		for (Map<String, Object> mapObject : table.getData()) {
			String secId = convertSecId((String) mapObject.get("secId"), true);
			mapObject.put("secId", secId);
			SmdcHandicap handicap = MapUtil.toBean(SmdcHandicap.class, mapObject);
			if(handicap.getPrice()==0) continue;
			if (handicap.getTurnoverVol() == 0) {
				handicap.setOpen(false);
				handicap.setOpenPrice(handicap.getPreClosePrice());
				handicap.setHighPrice(handicap.getPreClosePrice());
				handicap.setLowPrice(handicap.getPreClosePrice());
				handicap.setPrice(handicap.getPreClosePrice());
			}
			handicap.setPriceChange(handicap.getPrice()-handicap.getPreClosePrice());
			handicap.setPercent(handicap.getPriceChange()/handicap.getPreClosePrice());
			String dateTime = mapObject.get("date")+" "+mapObject.get("time");
			handicap.setUpdateTime(DateUtil.toDate(dateTime, "yyyy-MM-dd HH:mm:ss"));
			handicapList.add(handicap);
		}
		return handicapList;
	}

	private String parseToList(String response) {
		response = response.replaceAll("var hq_str_","");
		response = response.replaceAll("\"", "").replaceAll("=", ",");
		return response.replaceAll(";", "");
	}

	protected String convertSecId(String secId, boolean isToLocal){
		if(isToLocal){
			String ticker = secId.substring(2);
			String exchange = secId.substring(0,2);
			if(exchange.equals("sh")) {
				exchange = "ss";
			}
			return ticker+"."+exchange;
		}else{
			int index=secId.indexOf(".");
			String ticker = secId.substring(0,index);
			String exchange = secId.substring(index+1);
			if(exchange.equals("ss")) {
				exchange = "sh";
			}
			return exchange+ticker;
		}
	}
	
	
	@Override
	public List<SmdcMarketDaily> update(SmdcSecurity security, Date begin) {
		throw new RuntimeException("暂不支持");
	}

	@Override
	public List<SmdcMarketDaily> update(List<SmdcSecurity> securityList, List<Date> begin) {
		throw new RuntimeException("暂不支持");
	}

}
